package io.renren.modules.biz.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.biz.entity.CenterEntity;
import io.renren.modules.biz.entity.GrabPacketEntity;
import io.renren.modules.biz.entity.MemberEntity;
import io.renren.modules.biz.entity.RoomEntity;
import io.renren.modules.biz.service.CenterService;
import io.renren.modules.biz.service.GrabPacketService;
import io.renren.modules.biz.service.MemberService;
import io.renren.modules.biz.service.RoomService;
import io.renren.modules.biz.service.SendPacketService;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * 抢红包会员表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-09-03 14:51:31
 */
@RestController
@RequestMapping("biz/grabpacket")
@SuppressWarnings("unchecked")
public class GrabPacketController {
	@Autowired
	private GrabPacketService grabPacketService;

	@Autowired
	private MemberService memberService;

	@Autowired
	private RoomService roomService;

	@Autowired
	private CenterService centerService;

	@Autowired
	private SendPacketService sendPacketService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("biz:grabpacket:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = grabPacketService.queryPage(params);
		List<GrabPacketEntity> list = (List<GrabPacketEntity>) page.getList();
		if (CollectionUtils.isNotEmpty(list)) {
			for (GrabPacketEntity grabPacketEntity : list) {
				MemberEntity entity = memberService.selectOne(new EntityWrapper<MemberEntity>().setSqlSelect("head_img", "mobile").eq("id", grabPacketEntity.getGrabMemberId()));
				grabPacketEntity.setHeadImg(entity.getHeadImg());
				grabPacketEntity.setMobile(entity.getMobile());

				RoomEntity roomEntity = roomService.selectOne(new EntityWrapper<RoomEntity>().setSqlSelect("room_name", "id", "center_id").eq("id", grabPacketEntity.getRoomId()));
				CenterEntity centerEntity = centerService.selectOne(new EntityWrapper<CenterEntity>().setSqlSelect("center_name").eq("id", roomEntity.getCenterId()));
				grabPacketEntity.setRoomName(centerEntity.getCenterName() + "-" + roomEntity.getRoomName());
			}
		}

		return R.ok().put("page", page);
	}

	@RequestMapping("/list1")
	@RequiresPermissions("biz:grabpacket:list")
	public R list1(@RequestParam Map<String, Object> params) {
		PageUtils page = grabPacketService.queryPage(params);
		List<GrabPacketEntity> list = (List<GrabPacketEntity>) page.getList();
		if (CollectionUtils.isNotEmpty(list)) {
			for (GrabPacketEntity grabPacketEntity : list) {
				MemberEntity entity = memberService.selectOne(new EntityWrapper<MemberEntity>().setSqlSelect("head_img", "mobile").eq("id", grabPacketEntity.getGrabMemberId()));
				grabPacketEntity.setHeadImg(entity.getHeadImg());
				grabPacketEntity.setMobile(entity.getMobile());
			}
		}

		return R.ok().put("page", page);
	}

}
